package com.nuanshui.heatedloan.extension;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class CustomHttpResponse extends HttpServletResponseWrapper {

	private HttpServletResponse response;
	private HttpServletRequest request;
	private boolean isClearCookie = true;
	
	public boolean isClearCookie() {
		return isClearCookie;
	}

	public void setClearCookie(boolean isClearCookie) {
		this.isClearCookie = isClearCookie;
	}
    
	
	public CustomHttpResponse(HttpServletResponse response,HttpServletRequest request) {
		super(response);
		this.response = response;
		this.request =request;
	}
	
	public void addOldSessionCookie(Cookie cookie) {
		response.addCookie(cookie);
	}
	
	@Override
	public void addCookie(Cookie cookie) {
		cookie.getName();
		request.getRequestURI();
		if(!isClearCookie){
			return;
		}
		super.addCookie(cookie);
		String value = cookie.getValue();
		if(value == null) {
			response.addHeader(cookie.getName(), "");	
		}else  {
			response.addHeader(cookie.getName(),value);
		}
		
	}
	
	@Override
	public void addHeader(String name, String value) {
		if("__not_clear_cookies".equals(name)) {
			isClearCookie = false;
			return;
		}
		if("clear_cookies".equals(name)) {
			isClearCookie = true;
			return;
		}
		response.addHeader(name, value);
	}
	
	@Override
	public void setHeader(String name, String value) {
		response.setHeader(name, value);
	}
	

	
}
